﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo._7C;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Abbinamenti : XForm
    {
        int CKBatchId = -1;
        public Abbinamenti(int ckBatchId)
        {
            InitializeComponent();
            TAM.GPabbTableAdapter = new Datasets.MyDataTableAdapters.GPabbTableAdapter();
            this.CKBatchId = ckBatchId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
        }

        BindingSource bs;
        private void Abbinamenti_Load(object sender, EventArgs e)
        {

            ds.GPabb.Columns.Add("Adet", typeof(int));
            ds.GPabb.Columns.Add("AtelyeId", typeof(int));
            bs = new BindingSource(ds, "GPabb");
            AddBsToList(bs);
            InitGridView(gridView1, bs);
            GetDetails("GPabb", "GetHelper", "GPabb", CKBatchId);
            //TAM.GPabbTableAdapter.Fill(ds.GPabb);

            //gridView1.YanYanaGetir("Adet", "Model"); //ADET sütunu sonradan eklendiği için çalışmadı
            //gridView1.Columns["Adet"].VisibleIndex = gridView1.Columns["Scan"].VisibleIndex;
            gridView1.YanYanaGetirSoluna("Scan", "Adet", "AtelyeId");
            //gridView1.SetCaptions("UV:Ü. V.");
            //gridView1.Columns["UV"].ToolTip = "Üretime Verildi veya Üretildi";

            gridView1.FormatAsNumber(0, "Adet");
            gridView1.AddSummary("Adet");

            btnKaydet.Visibility = !ytk(55) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView1.BestFitColumns();

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("PS-SN garnilere GRN. öneki ekle", GrnEkle);
            //if (AmI(17, 92, 120))
            //gm.GridMenu.AddMenu("CK formunu güncelle", CKguncelle);

            btnUpdate.Visibility = ytk(77) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            btnUpdate.ItemClick += btnUpdate_ItemClick;


        }

        void btnUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataSet dsx = ExecuteDataSet("Get7cTakipYeni", CKBatchId, 0, 0, 1);
            if (dsx.Tables.Count > 2)
                XtraMessageBox.Show("Açmanız gereken kartlar var. Açtıktan sonra tekrar deneyin.");
            else
                if (dsx.Tables[0].Rows.Count > 0)
                    using (OlusanFarklar of = new OlusanFarklar(dsx.Tables[0]))
                        of.ShowDialog();

            //Notify("Ara tablolar başarı ile güncellendi.");
        }

        string PrefixedCode(string x)
        {
            if (x != null)
            {
                if (x.StartsWith("PS0002UN") ||
                x.StartsWith("SN0001UN") ||
                x.StartsWith("JNL070UN"))
                    return "GRN." + x;

                if (x.StartsWith("PF0091UN"))
                    return "GRN." + x.Replace("PF0091UN", "PS0002UN");
            }
            return x;
        }

        private void GrnEkle(object sender, EventArgs e)
        {
            foreach (MyData.GPabbRow dr in ds.GPabb)
            {
                if (!dr.IsINT1Null())
                    dr.INT1 = PrefixedCode(dr.INT1);

                if (!dr.IsINT2Null())
                    dr.INT2 = PrefixedCode(dr.INT2);

                if (!dr.IsINT3Null())
                    dr.INT3 = PrefixedCode(dr.INT3);

                if (!dr.IsINT4Null())
                    dr.INT4 = PrefixedCode(dr.INT4);

                if (!dr.IsINT5Null())
                    dr.INT5 = PrefixedCode(dr.INT5);

                if (!dr.IsProfiloTexNull())
                    dr.ProfiloTex = PrefixedCode(dr.ProfiloTex);

                if (!dr.IsGuidNull())
                    dr.Guid = PrefixedCode(dr.Guid);
            }
        }


        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            ExecuteNonQuery("UpdTableModify", "GPabb", WhoAmI);
            Save();
        }
    }
}